//
// Created by heming on 24-3-11.
//
#include "caes/aes.h"

#include <iostream>
#include <fstream>
#include <ostream>
#include <string>

using namespace std;







//ftaes enc plain_filename enc_filename
//ftaes dec enc_filename plain_filename
int main(int argc, char *argv[])
{
    if(argc != 4) {
        std::cout << "加密文件用法：" << std::endl;
        std::cout << "usage:command_aes enc plain_filename enc_filename" << endl;

        std::cout << "解密文件用法：" << std::endl;
        std::cout << "usage:command_aes dec enc_filename plain_filename " << endl;
        return 0;
    }
    else{
        auto key = gen_key("hello,what's up");
        std::string method(argv[1]);
        if(method.compare("enc")==0){
            string plain_filename(argv[2]);
            string enc_filename(argv[3]);
           if(encfile(plain_filename,enc_filename) > 0){
               cout<<"enc file failed,please check the plain_file is exists!!"<<std::endl;
           }
           else
           {
               cout<<"enc file is ok!!"<<endl;

           }
//
//
//            ifstream in;
//            in.open(plain_filename,ios::binary);
//            if (!in.good()) {
//                cout << "bad read!" << std::endl;
//                cout<<"please check if exists the file!!"<< std::endl;
//                return 0;
//            }
//            in.seekg(0, ios::end);
//            size_t size = in.tellg();
//            char* buff = new char[size];
//            in.seekg(0, ios::beg);
//            if (!in.read(buff, size)) {
//                cout << "bad read!" << std::endl;
//                return 0;
//            }
//            in.close();
//
//            //加密
//            string enc_filename(argv[3]);
//
//            auto enc = encrypt(key, buff, size);
//            ofstream ot;
//            ot.open(enc_filename, ios::binary);
//            ot.write(enc.data(), enc.size());
//            ot.close();

            return 0;

        }
        //"usage:command_aes dec enc_filename plain_filename "
        if(method.compare("dec")==0){
            string enc_filename(argv[2]);
            string plain_filename(argv[3]);
            if(decfile(enc_filename,plain_filename) ==0){
                cout<< "dec file ok!!"<<endl;
            }
            else{
                cout<<"dec file fail,please check the enc file exists"<<endl;
            }

            return 0;
//            string enc_filename(argv[2]);
//            string plain_filename(argv[3]);
//
//            ifstream in;
//            in.open(enc_filename,ios::binary);
//            if (!in.good()) {
//                cout << "bad read!please check if exists the file!!" << std::endl;
//                cout<<"please check if exists the file!!"<< std::endl;
//                return 0;
//            }
//            in.seekg(0, ios::end);
//            size_t size = in.tellg();
//            char* buff = new char[size];
//            in.seekg(0, ios::beg);
//            if (!in.read(buff, size)) {
//                cout << "bad read!" << std::endl;
//                return 0;
//            }
//            in.close();
//
//
//            auto dec=decrypt(key,buff,size);
//            ofstream ot;
//            ot.open(plain_filename,ios::binary);
//            ot.write(dec.data(),dec.size());
//            ot.close();
        }


    }




}